package com.borqs.samples;

import android.view.*;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.text.format.Time;
import android.util.*;
import android.app.*;
import android.content.*;
import android.database.Cursor;

import java.util.*;
import java.util.zip.Inflater;

import android.net.Uri;
import android.os.*;

public class SampleTest extends Activity implements OnItemClickListener {
    private final String TAG = "SampleTest";

    private Button beginButton, queryButton;
    private ListView mListView = null;

    private SampleService mBoundService;

    private Cursor mCursor = null;
    private ArrayList<Integer> mIDlist = null;

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder service) {
            mBoundService = ((SampleService.SamplingBinder) service)
                    .getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName className) {
            mBoundService = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        beginButton = (Button) findViewById(R.id.begin_button);
        queryButton = (Button) findViewById(R.id.query);

        mListView = (ListView) findViewById(R.id.sessions_list);

        startService(new Intent(SampleTest.this, SampleService.class));

        mListView.setOnItemClickListener(this);
        beginButton.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                // stopService(new Intent(SampleTest.this,SampleService.class));
                Log.d(TAG, "button clicked ...");
                // ps.sample(0);
                // testBatteryStats();
                // showProcessInfo();
                // testAddActionRule();
                // testGetActionInfo();
            }
        });

        queryButton.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                Cursor c = query(DBProvider.SessionsTable.CONTENT_URI, null, 0);
                mCursor = c;
                setListAdapter(c);
            }
        });
    }

    private void setListAdapter(Cursor c) {

        String[] from = new String[] { DBProvider.SessionsTable.START_TIME,
                DBProvider.SessionsTable.START_AVERAGE,
                DBProvider.SessionsTable.START_CHARGE,
                DBProvider.SessionsTable.STOP_AVERAGE };
        int[] to = new int[] { R.id.start_time, R.id.start_average_v,
                R.id.start_charge, R.id.stop_time };

        SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
                R.layout.sessions_list_item, c, from, to, 0);

        mListView.setAdapter(simpleCursorAdapter);
    }

    private Cursor query(Uri uri, String whereRow, int where) {
        Cursor c = null;
        if (null != whereRow) {
            c = getContentResolver().query(uri, null, whereRow + "=?",
                    new String[] { where + "" }, "_id ASC");
        } else {
            c = getContentResolver().query(uri, null, null, null, "_id ASC");
        }

        if (c != null && c.moveToFirst()) {
            // String leader = c
            // .getString(c
            // .getColumnIndexOrThrow(DBProvider.SessionsTable.START_AVERAGE));
            // Log.i(TAG, "leader.name=" + leader);
            return c;
        } else {
            Log.i(TAG, "query failure!");
        }
        return null;
    }

    /*
     * @Override public void onResume(){ super.onResume(); bindService(new
     * Intent(this,WakingService.class),mConnection,Context.BIND_AUTO_CREATE); }
     * 
     * @Override public void onStop(){ super.onStop();
     * unbindService(mConnection); }
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        unbindService(mConnection);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        int _id = 0;
        mCursor.moveToFirst();
        mCursor.move(position);
        _id = mCursor.getInt(mCursor
                .getColumnIndex(DBProvider.SessionsTable._ID));
        Toast.makeText(this, "_id===" + _id, 100).show();
        Intent intent = new Intent(this, sampleActivity.class);
        intent.putExtra("1", _id);
        startActivity(intent);
    }
}
